import Reqwest from '../../utils/Reqwest';
import {PageBean} from './PageBean';
import {base, assign, redirect}  from '../base/Base';
import {ResponseDto} from '../base/BaseDTO';
declare let GLOBAL: any;

export class PageController {
    path = `${GLOBAL.baseApiUrl}/page/api`;

    /**
     * list
     * 查询Page列表
     * @param {*} [params] 
     * @param {any} [options={}] 
     * @returns {Promise<any>} 
     * 
     * @memberOf PageController
     */
    searchPage(params?: any, options = {}): Promise<any> {
        return new Reqwest(assign({
            url: `${this.path}/list.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * getPage
     * 根据ID查询Page信息
     * @param {{
     *         id: number
     *     }} [params] 
     * @param {any} [options={}] 
     * @returns {Promise<ResponseDto<PageBean<any>>>} 
     * 
     * @memberOf PageController
     */
    getPage(params?: {
        id: number
    }, options = {}): Promise<ResponseDto<PageBean<any>>> {
        return new Reqwest(assign({
            url: `${this.path}/view.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * editPage
     * 修改Page
     * @param {PageBean<any>} params 
     * @param {any} [options={}] 
     * @returns {Promise<BuBean<any>>} 
     * 
     * @memberOf BuController
     */
    editPage(params: PageBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/edit.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * addPage
     * 新增Page
     * @param {PageBean<any>} params 
     * @param {any} [options={}] 
     * @returns {Promise<BuBean<any>>} 
     * 
     * @memberOf BuController
     */
    addPage(params: PageBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/add.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * deletePage
     * 删除Page
     * @param {{
     *         id: number
     *     }} [params] 
     * @param {any} [options={}] 
     * @returns {Promise<any>} 
     * 
     * @memberOf PageController
     */
    deletePage(params?: {
        id: number
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/delete.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }
}